<?php

/*===============微信商城=================*/
/*----------------------------------------*/
/* $Author: 疯中追风                      */
/*----------------------------------------*/
/* $Time: 2017/10/16 16:06                */
/*----------------------------------------*/
/*===============用户订单表===============*/

namespace   Home\Controller;
use  Think\Controller;

class OrderController  extends  Controller{
	//订单页
	public function order(){
		//获取用户id
		$id = cookie('id');
		$address_id = I('get.address_id');

		if($address_id){

			//将所有的默认收货地址改为false
			$count = M('User_address') -> select();
			$model = M('User_address');
			for($i = 0;$i < count($count);$i ++){
				$model -> is_default = 0;
				$model -> where("address_id =".$count[$i]['address_id']." and user_id = $id") -> save();
			}
			$model -> is_default = 1;
			$result = M('User_address') -> where("address_id = $address_id") -> save();
				//查询默认的收货地址
				$default_address = M('User_address') -> where("user_id = $id "." and is_default = 1") -> find();
				//获取省级,市级,区级,镇级以及详情地址
				$regin_model = M('Region_1');
				//将id查询出城市名字并替换。
				$default_address['province'] = $regin_model -> where("id = ".$default_address['province']) -> find()['name'];
				$default_address['city']     = $regin_model -> where("id = ".$default_address['city']) -> find()['name'];
				$default_address['district']  = $regin_model -> where("id = ".$default_address['district']) -> find()['name']		;
				$default_address['twon']   	= $regin_model -> where("id = ".$default_address['twon']) -> find()['name'];
			 	$this -> assign('default_address',$default_address);
		}else{

			//先查询收货地址是否为空
			if(M('User_address') -> where(array('user_id'=>$id)) -> select()!=NULL){
				//查询默认的收货地址
				$default_address = M('User_address') -> where("user_id =".$id." and is_default = 1") -> find();
				//获取省级,市级,区级,镇级以及详情地址
				if($default_address){
					$regin_model = M('Region_1');
					//将id查询出城市名字并替换。
					$default_address['province'] = $regin_model -> where("id = ".$default_address['province']) -> find()['name'];
					$default_address['city']     = $regin_model -> where("id = ".$default_address['city']) -> find()['name'];
					$default_address['district']  = $regin_model -> where("id = ".$default_address['district']) -> find()['name']		;
					$default_address['twon']   	= $regin_model -> where("id = ".$default_address['twon']) -> find()['name'];
			 		$this -> assign('default_address',$default_address);
				}else{
					$result = M('User_address') -> find();
					M('User_address') -> is_default = 1;
					M('User_address') -> where("address_id =".$result['address_id']) -> save();
					redirect(U("Home/Order/order"));
				}
				
			}else{
				//重定向地址
				redirect(U("Home/User/add_address"));
			}
		}
		
		//在购物车中查询所有订单
		$all_order = M('Cart') -> where("user_id = $id  and  is_order = 1") -> select();
		//查询商品总价
		$all_data = M('Cart') -> where("user_id = $id and is_order = 1") -> select();
		$total_price = 0;
		//邮费价钱
		$post_price = 0;
		foreach ($all_data as $key => $value) {
			$total_price += (int)$value["goods_num"] * (int)$value["goods_price"];
			$post_price  += (int)$value["post_price"];
		}
		//邮费加商品总价
		$this -> assign("all_order",$all_order);
		$this -> assign("total_price",$total_price);
		$this -> assign("post_price",$post_price);
		$this -> assign("all_price",$total_price+$post_price);
		$this -> display();
	}


	//确认订单
	public function enter_order(){
		$id = cookie('id');
		//在购物车中查询订单
		$result = M("Cart") -> where(array("id"=>$id,"is_order"=>1)) -> select();

		//向订单中添加收货地址
		$regin_model = M('Region_1');
		$user_address = M('User_address') -> where(array("user_id"=>$id,"is_default"=>1)) -> find();
		//将id查询出城市名字并替换。
		$count = count($result);

		//将所有的商品属性添加到订单表中
		$order = array();
		for($i = 0;$i < $count;$i++){
			$order[$i]['order_sn']        = "20171029".$result[$i]['cart_id'];
			$order[$i]['user_id']         = $id;
			$order[$i]['order_status']    = 0;
			$order[$i]['shipping_status'] = 0;
			$order[$i]['pay_status'] 	  = 0;
			$order[$i]['shouhuo_name']    = $user_address['consignee'];
			$order[$i]['province']        = $regin_model -> where("id = ".$user_address['province']) -> find()['name'];
			$order[$i]['city']            = $regin_model -> where("id = ".$user_address['city']) -> find()['name'];
			$order[$i]['district']        = $regin_model -> where("id = ".$user_address['district']) -> find()['name'];
			$order[$i]['twon']   	      = $regin_model -> where("id = ".$user_address['twon']) -> find()['name'];
			$order[$i]['address']         = $user_address['address'];
			$order[$i]['zipcode']         = $user_address['postalcode'];
			$order[$i]['mobile']          = $user_address['mobile'];
			$order[$i]['goods_name']      = $result[$i]['goods_name'];
			$order[$i]['goods_price']     = $result[$i]['goods_price'];
			$order[$i]['goods_image']     = $result[$i]['goods_url'];
			$order[$i]['goods_num']       = $result[$i]['goods_num'];
			$order[$i]['shipping_price']  = $result[$i]['post_price'];
			$order[$i]['goods_total_price']  = (int)$result[$i]['goods_price'] * (int)$result[$i]['goods_num'] + (int)$result[$i]['post_price'];
		}


		//实力化订单表
		$count2 = count($order);
		for($i = 0 ;$i < $count2; $i++){
			//判断订单表中是否存在数据
			if(M('Order') -> where($order[$i]) -> find()){
				continue;
			}else{
				M('Order') -> add($order[$i]);
			}
		}
	}
}